﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MongoServerBasedTests.cs" company="Data Ductus Malmö AB">
//   (c) Data Ductua Malmö AB
// </copyright>
// <summary>
// Released under Apache License, Version 2.0, January 2004, http://www.apache.org/licenses/
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace DataDuctus.MongoDb.Serialization.Tests
{
  using System;
  using System.Diagnostics;
  using System.IO;

  using DataDuctus.MongoDb.Embedding.Server;

  using Microsoft.VisualStudio.TestTools.UnitTesting;

  using MongoDB.Driver;

  /// <summary>
  /// The mongo server based tests.
  /// </summary>
  [TestClass]
  public abstract class MongoServerBasedTests
  {
    #region Constants and Fields

    /// <summary>
    /// The use embedded server.
    /// </summary>
    public const bool UseEmbeddedServer = true;

    #endregion

    #region Properties

    /// <summary>
    /// Gets or sets TestContext.
    /// </summary>
    public TestContext TestContext { get; set; }

    /// <summary>
    /// Gets ConnectionUrl.
    /// </summary>
    protected string ConnectionUrl { get; private set; }

    /// <summary>
    /// Gets EmbeddedServer.
    /// </summary>
    protected EmbeddedMongoDbServer EmbeddedServer { get; private set; }

    /// <summary>
    /// Gets Server.
    /// </summary>
    protected MongoServer Server { get; private set; }

    /// <summary>
    /// Gets TestDataBaseName.
    /// </summary>
    protected string TestDataBaseName { get; private set; }

    /// <summary>
    /// Gets TestDatabase.
    /// </summary>
    protected MongoDatabase TestDatabase { get; private set; }

    #endregion

    #region Public Methods

    /// <summary>
    /// The cleanup test.
    /// </summary>
    [TestCleanup]
    public void CleanupTest()
    {
      MongoServer server = this.Server;
      this.Server = null;

      if (null != server)
      {
        try
        {
          server.Disconnect();
        }
        catch (Exception e)
        {
          Trace.TraceError(string.Format("Exception while disconnecting from mongoServer msg = {0}", e.Message));
        }
      }

      EmbeddedMongoDbServer embeddedServer = this.EmbeddedServer;
      this.EmbeddedServer = null;

      if (null != embeddedServer)
      {
        embeddedServer.Dispose();
      }

      this.ConnectionUrl = null;
      this.TestDataBaseName = null;
    }

    /// <summary>
    /// The init test.
    /// </summary>
    [TestInitialize]
    public void InitTest()
    {
      // ReSharper disable ConditionIsAlwaysTrueOrFalse
      if (UseEmbeddedServer)
      {
        // ReSharper restore ConditionIsAlwaysTrueOrFalse
        var embeddedServer = new EmbeddedMongoDbServer
          {
            BinPath = Directory.GetCurrentDirectory(), 
            DbPath = Path.Combine(Directory.GetCurrentDirectory(), "theDb"), 
            LogFilePath = Path.Combine(Directory.GetCurrentDirectory(), "mongodb.log"), 
            BindIp = "127.0.0.1"
          };

        embeddedServer.Open();

        this.EmbeddedServer = embeddedServer;
      }

      this.ConnectionUrl = "mongodb://localhost/?safe=true";
      this.TestDataBaseName = "testDb";

      this.Server = MongoServer.Create(this.ConnectionUrl);
      this.TestDatabase = this.Server.GetDatabase(this.TestDataBaseName);
    }

    #endregion
  }
}